//
//  QiNiuUploadTool.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/6.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "QiNiuUploadTool.h"
#import <QiniuSDK.h>

@interface QiNiuUploadTool ()


@end

@implementation QiNiuUploadTool

- (instancetype)init {
    self = [super init];
    if (self) {
//        self.imageKey = [NSMutableArray array];
        self.orUpdata = NO;
    }
    return self;
}

- (void)uploadImages:(NSArray *)images atIndex:(NSInteger)index token:(NSString *)token uploadManager:(QNUploadManager *)uploadManager keys:(NSMutableArray *)keys{
    if (images.count <= 0) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    UIImage *image = images[index];
    __block NSInteger imageIndex = index;
    //图片转化是否有损
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    //时间戳
    NSTimeInterval time= [[NSDate new] timeIntervalSince1970];
    //图片路径拼接
    NSString *filename = [NSString stringWithFormat:@"%@_%ld_%.f.%@",@"status",686734963504054272,time,@"jpg"];
    [uploadManager putData:data key:filename token:token
                  complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                      if (resp != nil) {
                          NSLog(@"上传成功--%@", resp);
                          NSString *imagekey = resp[@"key"];
                          
                          NSString *imageUrl = [NSString stringWithFormat:@"http://web.wohoney.com/%@", imagekey];
                          NSLog(@"第%ld张图片--%@", index, imageUrl);
                          NSMutableDictionary *dic = [self imageTransform:imagekey index:index];
                          if (dic == nil) {
                              
                          }else {
                             [keys addObject:dic];
                          }
                          
                          imageIndex++;
                          if (imageIndex >= images.count) {
                              NSLog(@"上传完成");
                              weakSelf.imageKey = [NSMutableArray arrayWithArray:keys];
                              weakSelf.orUpdata = YES;
                              for (NSString *imgKey in keys) {
                                  NSLog(@"%@",imgKey);
                              }
                              return ;
                          }
                          [weakSelf uploadImages:images atIndex:imageIndex token:token uploadManager:uploadManager keys:keys];
                      }else {
                          NSLog(@"上传失败--%@", resp);
                      }
                      if (info.isOK) {
                          
                      }else {
                          NSLog(@"图片上传失败--%@", resp);
                      }
                  }
                    option:nil];
}

//将图片地址 以下标为Key 图片地址为Value 转化成字典 防止图片上传后顺序错乱
- (NSMutableDictionary *)imageTransform:(NSString *)imageKey index:(NSInteger)index {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSString *imageUrl;
    if (imageKey != nil && ![imageKey isEqualToString:@""]) {
         imageUrl = [NSString stringWithFormat:@"http://web.wohoney.com/%@", imageKey];
    }else {
        NSLog(@"七牛返回的图片Key不存在");
        return nil;
    }
    [dic setValue:imageUrl forKey:[NSString stringWithFormat:@"%ld", (long)index]];
    NSLog(@"拼接缓存后dic--%@", dic);
    return dic;
}

//获取所有图片key后遍历拼接 然后进行字典转化 最后返回一个数组
- (NSArray *)forImageURL:(NSMutableArray *)imageKey {
    NSMutableArray *array = [NSMutableArray array];
    if (imageKey.count > 0) {
        for (int i = 0; i < imageKey.count; i++) {
            NSDictionary *dic = [NSDictionary dictionary];
            NSString *key = imageKey[i];
            NSString *imageUrl = [NSString stringWithFormat:@"http://web.wohoney.com/%@", key];
            [dic setValue:imageUrl forKey:[NSString stringWithFormat:@"%d", i]];
            [array addObject:dic];
        }
        NSLog(@"数据源--%@", array);
        return [NSArray arrayWithArray:array];
    }else {
        return nil;
    }
}
@end
